package com.example.mannortycoon.controller;

import com.auth0.jwt.JWT;
import com.example.mannortycoon.entity.SysUserEntity;
import com.example.mannortycoon.service.SysUserRoleService;
import com.example.mannortycoon.service.SysUserService;
import com.example.mannortycoon.service.SysUserTokenService;
import com.example.mannortycoon.uitl.JWTUtils;
import com.example.mannortycoon.uitl.PageUtils;
import com.example.mannortycoon.uitl.R;
import com.example.mannortycoon.vo.LoginInfo;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@ApiIgnore
@RestController
@RequestMapping("/sys/user")
public class SysUserController {

    private final SysUserService sysUserService;

    private final SysUserRoleService sysUserRoleService;

    private final SysUserTokenService sysUserTokenService;

    @Autowired
    public SysUserController(SysUserService sysUserService,SysUserRoleService sysUserRoleService,SysUserTokenService sysUserTokenService){
        this.sysUserService = sysUserService;
        this.sysUserRoleService = sysUserRoleService;
        this.sysUserTokenService = sysUserTokenService;
    }

    /**
     * 列表
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = sysUserService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 登录
     */
    @PostMapping("/login")
    public R login(@RequestBody LoginInfo loginInfo){
        SysUserEntity user = sysUserService.queryByUserName(loginInfo.getUsername());

        if(user == null || !user.getPassword().equals(new Sha256Hash(loginInfo.getPassword(), user.getSalt()).toHex())) {
            return R.error("账号或密码不正确");
        }

        //账号锁定
        if(user.getStatus() == 0){
            return R.error("账号已被锁定,请联系管理员");
        }

        //生成token，并保存到数据库
        return sysUserTokenService.createToken(user.getUserId());
    }

    /**
     * 获取用户信息
     */
    @GetMapping("/info")
    public R info(HttpServletRequest request){
        String token = request.getHeader("token");
        Long userId = sysUserTokenService.getUserIdByToken(token);
        if(userId == null){
            return R.error(401,"token不存在或已失效");
        }
        SysUserEntity user = sysUserService.getById(userId);
        return R.ok().put("user",user);
    }

    /**
     * 根据id获取用户信息
     */
    @GetMapping("/info/{userId}")
    public R info(@PathVariable("userId") Long userId){
        SysUserEntity user = sysUserService.getById(userId);

        //获取用户所属的角色列表
        List<Long> roleIdList = sysUserRoleService.queryRoleIdList(userId);
        user.setRoleIdList(roleIdList);
        return R.ok().put("user", user);
    }

    /**
     * 保存用户
     */
    @PostMapping("/save")
    public R save(@RequestBody SysUserEntity user){
        user.setCreateUserId(1L);
        sysUserService.saveUser(user);
        return R.ok();
    }

    /**
     * 保存用户
     */
    @PostMapping("/update")
    public R update(@RequestBody SysUserEntity user){
        user.setCreateUserId(1L);
        sysUserService.update(user);
        return R.ok();
    }

    /**
     * 退出登录
     */
    @PostMapping("/logout")
    public R logout(HttpServletRequest request){
        Long userId = JWTUtils.getId(request.getHeader("token"));
        sysUserTokenService.logout(userId);
        return R.ok();
    }
}
